#include "warninginformation.h"
#include "ui_warninginformation.h"
#include"alarmalerttone.h"
#include "warningstatisticsinterface.h"
#include"monitordevice.h"
Q_GLOBAL_STATIC(WarningInformation,theInstance);

WarningInformation::WarningInformation(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::WarningInformation)
{
    ui->setupUi(this);
    this->setWindowTitle("报警信息界面");
    this->setWindowIcon(QIcon(":res/res/Alarm.ico"));
    ui->plainTextEditWarningInformation->setFont(QFont("Microsoft YaHei", 14));
    initSensorWarningInformation(Data);
    connect(MonitorDevice::instance(),&MonitorDevice::sendAirTemperatureToWarningInformation,this,&WarningInformation::handleWarningInformation);
    connect(MonitorDevice::instance(),&MonitorDevice::sendAirHumidityToWarningInformation,this,&WarningInformation::handleWarningInformation);
    connect(MonitorDevice::instance(),&MonitorDevice::sendLightIntensityToWarningInformation,this,&WarningInformation::handleWarningInformation);
    connect(MonitorDevice::instance(),&MonitorDevice::sendCO2ConcentrationToWarningInformation,this,&WarningInformation::handleWarningInformation);
    connect(MonitorDevice::instance(),&MonitorDevice::sendSoilMoistureToWarningInformation,this,&WarningInformation::handleWarningInformation);
}

WarningInformation *WarningInformation::instance()
{
return theInstance;
}

WarningInformation::~WarningInformation()
{
    delete ui;
}

bool WarningInformation::writeWarningInformation(QString enviromentalParameter, QString reasonOfWarning)
{

    QFile file(WarningInformationWorckPlace::WarningInformationFile);
    QString WarningInformation = QString(">> [%1]---%2---%3 \n")
        .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss.zzz"))
        .arg(enviromentalParameter).arg(reasonOfWarning);

    if (!file.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text))
    {
        qDebug()<<"打开文件失败";
        return false;
    }
    else
    {

        // 写入文件
           QTextStream stream(&file);
           stream.setCodec("UTF-8");
           stream << WarningInformation;
           file.close();
           return true;


    }





}

void WarningInformation::openWarningInformationFileExternal(const QString &filePath)
{
    // 检查文件是否存在
        if (!QFileInfo::exists(filePath)) {
            qWarning() << "日志文件不存在：" << filePath;
            return;
        }

        // 检查软件是否存在
           if (!QFileInfo::exists(WarningInformationWorckPlace::programPath)) {
               qDebug() << "指定软件未安装或路径错误";
               return;
           }

           // 启动程序
           QProcess process;
           bool success = process.startDetached(WarningInformationWorckPlace::programPath, {filePath});

           if (!success) {
               qDebug() << "启动失败，请检查权限或路径";
           }
}

bool WarningInformation::writePlainTextEdit(const QString &filePath)
{

    ui->plainTextEditWarningInformation->clear();//清空上一次内容

    QFile file(filePath);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Append | QIODevice::Text))
    {
        qDebug()<<"打开文件失败";
        return false;
    }
    else
    {


                file.flush();
                file.seek(0); // 关键！将指针移回文件开头
               // 读取内容
                QTextStream in(&file);
                in.setCodec("UTF-8"); // 设置编码
                QString content = in.readAll();
                qDebug()<<"content "<<content ;
                file.close();
                // 显示内容

               ui->plainTextEditWarningInformation->appendPlainText(content);
    }
    return true;
}

void WarningInformation::handleWarningInformation(int index, int status)
{
int column=1;
if(status==-1)
{
    column=2;
}

writeWarningInformation(Data[0][index],Data[column][index]);
}

void WarningInformation::on_pushButtonAlarmSoundSeting_clicked()
{
   AlarmAlertTone::instance()->show();
}


void WarningInformation::on_pushButtonClose_clicked()
{
    this->close();
}




void WarningInformation::on_pushButtonOPenWarningInformation_clicked()
{
  openWarningInformationFileExternal();
}


void WarningInformation::on_pushButtonAlarmStatistics_clicked()
{
WarningStatisticsInterface::instance()->RefreshWarningTimes();//刷新报警次数
WarningStatisticsInterface::instance()->show();
}
void WarningInformation::initSensorWarningInformation(QVector<QVector<QString>>& Data)
{
    Data=  {
                { "空气温度传感器",  "空气湿度传感器",  "光照传感器", "CO2浓度传感器", "土壤湿度传感器" },  // 第 0 行
                { "空气温度过高",    "空气湿度过高",   "光照强度过高", "CO2浓度过高" , "土壤湿度过高"},  // 第 1 行
                { "空气温度过低",    "空气湿度过低",   "光照强度过低", "CO2浓度过低" , "土壤湿度过低"}   // 第 2 行
           };
}
